<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="../js/axios.min.js" type="text/javascript"></script>
    <script src="../js/vue.js" type="text/javascript"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <title>找回密码</title>
</head>
<body>
<div id="app">
    <el-row style="margin-top: 150px;">
        <el-col :span="8" :offset="8">
            <el-form :model="registerForm" status-icon :rules="rules" ref="registerForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="registerForm.email" placeholder="请输入注册邮箱" @change="checkEmail()"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <el-input v-model="code" autocomplete="off" placeholder="验证码" @change="checkCode()"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="password">
                    <el-input type="password" v-model="registerForm.password" autocomplete="off" placeholder="新密码"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPassword">
                    <el-input type="password" v-model="registerForm.checkPassword" autocomplete="off" placeholder="再次输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="sendCode()" :disabled="use">发送验证码</el-button>
                    <el-button type="primary" @click="submitForm('registerForm')">提交</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data() {
            let checkEmail = (rule, value, callback) => {
                const data = /^([a-zA-Z0-9]+[-_\.]?)+@[a-zA-Z0-9]+\.[a-z]+$/;
                if (!value) {
                    return callback(new Error('邮箱不能为空'));
                }else if (!data.test(value)){
                    return callback(new Error('请输入正确格式的邮箱'));
                }
                callback()

            };
            let validatePassword = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.registerForm.checkPassword !== '') {
                        this.$refs.registerForm.validateField('checkPassword');
                    }
                    callback();
                }
            };
            let validatePassword2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.registerForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                registerForm: {
                    email: '',
                    password: '',
                    checkPassword: ''
                },
                rules: {
                    password: [
                        { validator: validatePassword, trigger: 'blur' }
                    ],
                    checkPassword: [
                        { validator: validatePassword2, trigger: 'blur' }
                    ],
                    email: [
                        { validator: checkEmail, trigger: 'blur' }
                    ]
                },
                code:"",
                use:false// 发送验证码的禁用状态
            };
        },
        methods: {
            sendCode(){
                axios.post("/account/sendCode?email="+this.registerForm.email).then(resp=>{
                        if(resp.status===resp.data.code){
                            this.use=true
                            this.$message({
                                type: 'success',
                                message: '发送成功 请留意查收验证码邮件'
                            });
                        }
                    })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("/account/resetPwd", this.registerForm).then(resp=>{
                            if(resp.status===resp.data.code){
                                this.$message({
                                    type: 'success',
                                    message: '重置成功 即将跳转登录页面'
                                });
                                setTimeout(() => {
                                    location.href = 'login.html'
                                }, 3000);
                            }else{
                                alert("重置失败 请重试！")
                            }
                        })
                    } else {
                        this.$message({
                            message: '请修改信息后再提交',
                            type: 'warning'
                        });
                        return false;
                    }
                });
            },
            checkEmail(){
                axios.get("/account/checkEmail?email="+this.registerForm.email).then(resp=>{
                    if(resp.status!==resp.data.code){
                        this.$message({
                            message: '该邮箱不存在',
                            type: 'warning'
                        });
                    }
                })
            },
            checkCode(){
                axios.get("/account/checkCode?email="+this.registerForm.email+"&code="+this.code).then(resp=>{
                    if(resp.status===resp.data.code){
                        this.$message({
                            type: 'success',
                            message: '验证码正确'
                        });
                    }else{
                        this.$message({
                            type: 'warning',
                            message: '验证码不正确'
                        });
                    }
                })
            }
        }
    })
</script>
</html>